package com.tools.config;


import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import lombok.AllArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@AllArgsConstructor
@Configuration
@EnableConfigurationProperties({UploadProperties.class, SystemProperties.class})
public class FilesConfiguration {
    UploadProperties properties;

    @Bean
    public AmazonS3 qiiuS3() {
        AWSStaticCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(
                new BasicAWSCredentials(properties.getAk(), properties.getSk()));

        AmazonS3 s3 = AmazonS3Client.builder()
                .withEndpointConfiguration(
                        new AwsClientBuilder.EndpointConfiguration(
                                properties.getS3Endpoint(), "cn-south-1"))
                .withCredentials(credentialsProvider)
                .disableChunkedEncoding()
                .withPathStyleAccessEnabled(false)
                .build();
        // String s3endpoint = properties.getS3Endpoint();
        // String accessKeyId = properties.getAk();
        // String accessKeySecret = properties.getSk();
        // AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials(accessKeyId, accessKeySecret));
        // s3.setEndpoint(s3endpoint);
        // S3ClientOptions options = new S3ClientOptions();
        // options.withChunkedEncodingDisabled(true);
        // s3.setS3ClientOptions(options);
        // for (Bucket bucket : s3.listBuckets()) {
        //     System.out.println(" - " + bucket.getName());
        // }
        // s3.setEndpoint(properties.getS3Endpoint());
        return s3;
    }
}
